package com.jic.veteran.service.impl;

import com.jic.veteran.common.pay.SybConstants;
import com.jic.veteran.common.pay.SybPayV2Util;
import com.jic.veteran.common.pay.SybUtil;
import com.jic.veteran.mapper.PayInfoMapper;
import com.jic.veteran.model.PayInfo;
import com.jic.veteran.service.DonatePayService;
import com.jic.veteran.service.UmsAdminCacheService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.TreeMap;

@Slf4j
@Service
public class DonatePayServiceImpl implements DonatePayService {

    @Autowired
    private UmsAdminCacheService umsAdminCacheService;
    @Autowired
    private PayInfoMapper payInfoMapper;

    @Override
    public Map<String, String> query(String reqsn,String trxid) {
        PayInfo payInfo = umsAdminCacheService.getPayInfo();
        if(null != payInfo) {
            TreeMap<String,String> params = new TreeMap<String,String>();
            params.put("cusid", payInfo.getCustId());
            params.put("appid", payInfo.getAppId());
            params.put("version", "11");
            params.put("reqsn", reqsn);
            params.put("trxid", trxid);
            params.put("randomstr", SybUtil.getValidatecode(8));
            params.put("signtype", SybConstants.SIGN_TYPE);
            String appkey = "";
            if(SybConstants.SIGN_TYPE.equals("RSA"))
                appkey = payInfo.getRsaPrivateKey();
            else if(SybConstants.SIGN_TYPE.equals("SM2"))
                appkey = payInfo.getSm2PrivateKey();
            else
                appkey = payInfo.getMd5Key();
            try {
                params.put("sign", SybUtil.unionSign(params,appkey, SybConstants.SIGN_TYPE));
            } catch (Exception e) {
                log.info("支付数据签名加密失败,{}",e.getMessage());
            }
            try {
                return SybPayV2Util.query(params,payInfo.getAppUrl());
            } catch (Exception e) {
                log.info("查询通联交易数据失败,{}",e.getMessage());
            }
        }
        return null;
    }

    @Override
    public String getUrl(String date,String custId) {
        PayInfo payInfo = payInfoMapper.getPayInfoByCustId(custId);
        if(null != payInfo) {
            TreeMap<String,String> params = new TreeMap<String,String>();

            params.put("cusid", custId);
            params.put("appid", payInfo.getAppId());
            params.put("date", date);
            params.put("signtype", SybConstants.SIGN_TYPE);
            params.put("randomstr", SybUtil.getValidatecode(8));
            String appkey = "";
            if(SybConstants.SIGN_TYPE.equals("RSA"))
                appkey = payInfo.getRsaPrivateKey();
            else if(SybConstants.SIGN_TYPE.equals("SM2"))
                appkey = payInfo.getSm2PrivateKey();
            else
                appkey = payInfo.getMd5Key();
            try {
                params.put("sign", SybUtil.unionSign(params,appkey, SybConstants.SIGN_TYPE));
            } catch (Exception e) {
                log.info("获取对账单下载地址签名加密失败,{}",e.getMessage());
            }
            try {
                return SybPayV2Util.getUrl(params,payInfo.getGetUrl());
            } catch (Exception e) {
                log.info("获取对账单下载地址,{}",e.getMessage());
            }
        }
        return null;
    }
}
